// Marty Stepp, CSE 142, Winter 2008 // // This program draws lines and boxes of stars. // This version uses even more parameterization, including // parameterizing a String for the character to print. // // Types in Java: // int integers // double real numbers // String text public class Stars2 { public static void main(String[] args) { drawLineOfStars(10 + 3); int x = 7; drawLineOfStars(x); drawLineOfStars(35); System.out.println(); drawBox("*", 10, 3); drawBox("+", 5, 4); drawBox(":", 35, 20); } // Prints the given character the given number of times. // For example, printCharacter("?", 3); prints 3 question marks. public static void printCharacter(String character, int count) { for (int i = 1; i <= count; i++) { System.out.print(character); } } // Prints a line containing the given number of stars. // For example, drawLineOfStars(8) prints 8 stars. public static void drawLineOfStars(int count) { printCharacter("*", count); System.out.println(); } // Prints a hollow box of the given width and height, // using the given character as the box's border. // For example, drawBox("*", 7, 5) makes a 7x5 box wrapped in *s. public static void drawBox(String character, int width, int height) { // top line printCharacter(character, width); System.out.println(); // middle hollow section for (int line = 1; line <= height - 2; line++) { System.out.print(character); printCharacter(" ", width - 2); System.out.println(character); // end the line } // bottom line printCharacter(character, width); System.out.println(); } }